home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / bbs / termv4.6 / extras / source / gtlayout-source.lha / LTP_LevelGadget.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  6.2 KB  |  299 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #ifdef DO_LEVEL_KIND
  15. VOID
  16. LTP_LevelGadgetDrawLabel(struct Gadget *Gadget,BOOL FullRefresh)
  17. {
  18.     LevelExtra    *Special = (LevelExtra *)Gadget->SpecialInfo;
  19.     LONG         Current;
  20.     ObjectNode    *Node = (ObjectNode *)Gadget->UserData;
  21.  
  22.     GetAttr(LVIA_Current,Special->LevelImage,(ULONG *)&Current);
  23.  
  24.     Current += Special->Plus;
  25.  
  26.     Special->Level = Current;
  27.  
  28.     if(Node->Label)
  29.     {
  30.         LayoutHandle *Handle = Special->Handle;
  31.  
  32.         if(FullRefresh)
  33.             LTP_DrawObjectLabel(Handle,Node);
  34.  
  35.         if(Special->LevelFormat)
  36.         {
  37.             UBYTE             Buffer[256];
  38.             struct RastPort *RPort = &Special->Handle->RPort;
  39.             LONG             LabelTop,LabelLeft;
  40.             LONG             LevelWidth,
  41.                              LevelLen;
  42.  
  43.             switch(Special->LevelPlace)
  44.             {
  45.                 case PLACETEXT_LEFT:
  46.  
  47.                     LabelLeft    = Node->Left - INTERWIDTH;
  48.                     LabelTop    = Node->Top + (Node->Height - Handle->GlyphHeight) / 2;
  49.                     break;
  50.  
  51.                 case PLACETEXT_RIGHT:
  52.  
  53.                     LabelLeft    = Node->Left + Node->Width + INTERWIDTH + Special->MaxLevelWidth;
  54.                     LabelTop    = Node->Top + (Node->Height - Handle->GlyphHeight) / 2;
  55.                     break;
  56.  
  57.                 case PLACETEXT_ABOVE:
  58.  
  59.                     LabelLeft    = Node->Left + (Node->Width - Special->MaxLevelWidth) / 2 + Special->MaxLevelWidth;
  60.                     LabelTop    = Node->Top - (Handle->GlyphHeight + INTERHEIGHT);
  61.                     break;
  62.  
  63.                 case PLACETEXT_BELOW:
  64.  
  65.                     LabelLeft    = Node->Left + (Node->Width - Special->MaxLevelWidth) / 2 + Special->MaxLevelWidth;
  66.                     LabelTop    = Node->Top + Node->Height + INTERHEIGHT;
  67.                     break;
  68.             }
  69.  
  70.             LTP_SetPens(RPort,Handle->TextPen,Handle->BackgroundPen,JAM2);
  71.  
  72.             if(Special->DispFunc)
  73.                 Current = (*Special->DispFunc)(Gadget,Current);
  74.  
  75.             SPrintf(Buffer,Special->LevelFormat,Current);
  76.  
  77.             LevelLen = strlen(Buffer);
  78.  
  79.             LevelWidth = TextLength(RPort,Buffer,LevelLen);
  80.  
  81.             LTP_PrintText(RPort,Buffer,LevelLen,LabelLeft - LevelWidth,LabelTop);
  82.  
  83.             if(LevelWidth < Special->MaxLevelWidth)
  84.                 LTP_EraseBox(RPort,LabelLeft - Special->MaxLevelWidth,LabelTop,Special->MaxLevelWidth - LevelWidth,Handle->GlyphHeight);
  85.         }
  86.     }
  87. }
  88.  
  89.  
  90. /*****************************************************************************/
  91.  
  92.  
  93. ULONG
  94. LTP_LevelGadgetRender(struct Gadget *Gadget,struct gpRender *RenderMsg)
  95. {
  96.     LevelExtra *Special = (LevelExtra *)Gadget->SpecialInfo;
  97.  
  98.     DrawImageState(RenderMsg->gpr_RPort,Special->LevelImage,Gadget->LeftEdge,Gadget->TopEdge,Gadget->Flags & GFLG_DISABLED ? IDS_DISABLED : IDS_NORMAL,Special->Handle->DrawInfo);
  99.  
  100.     LTP_LevelGadgetDrawLabel(Gadget,RenderMsg->gpr_Redraw == GREDRAW_REDRAW);
  101.  
  102.     return(TRUE);
  103. }
  104.  
  105.  
  106. /*****************************************************************************/
  107.  
  108.  
  109. ULONG
  110. LTP_LevelGadgetGoActive(struct Gadget *Gadget,struct gpInput *InputMsg)
  111. {
  112.     LevelExtra        *Special = Gadget->SpecialInfo;
  113.     LONG             Left;
  114.     ULONG             Position,
  115.                      KnobWidth,
  116.                      Current,
  117.                      Max;
  118.     LONG             State;
  119.     struct RastPort    *RPort;
  120.  
  121.     GetAttr(LVIA_Position,    Special->LevelImage,&Position);
  122.     GetAttr(LVIA_Current,    Special->LevelImage,&Current);
  123.     GetAttr(LVIA_KnobWidth, Special->LevelImage,&KnobWidth);
  124.     GetAttr(LVIA_Max,        Special->LevelImage,&Max);
  125.  
  126.     Left = InputMsg->gpi_Mouse.X;
  127.  
  128.     if(Left < Position)
  129.     {
  130.         if(Current > 0)
  131.         {
  132.             State = IDS_NORMAL;
  133.  
  134.             Current--;
  135.         }
  136.     }
  137.     else
  138.     {
  139.         if(Left > KnobWidth + Position + KnobWidth)
  140.         {
  141.             if(Current < Max)
  142.             {
  143.                 State = IDS_NORMAL;
  144.  
  145.                 Current++;
  146.             }
  147.         }
  148.         else
  149.         {
  150.             State = IDS_SELECTED;
  151.  
  152.             Special->MidOffset = KnobWidth + Position - Left;
  153.         }
  154.     }
  155.  
  156.     if(RPort = ObtainGIRPort(InputMsg->gpi_GInfo))
  157.     {
  158.         DrawImageState(RPort,Special->LevelImage,Gadget->LeftEdge,Gadget->TopEdge,State,Special->Handle->DrawInfo);
  159.  
  160.         ReleaseGIRPort(RPort);
  161.     }
  162.  
  163.     if(State == IDS_NORMAL)
  164.     {
  165.         SetAttrs(Special->LevelImage,LVIA_Current,Current,TAG_DONE);
  166.  
  167.         LTP_LevelGadgetDrawLabel(Gadget,FALSE);
  168.  
  169.         *InputMsg->gpi_Termination = FALSE;
  170.  
  171.         return(GMR_NOREUSE | GMR_VERIFY);
  172.     }
  173.     else
  174.         return(GMR_MEACTIVE);
  175. }
  176.  
  177.  
  178. /*****************************************************************************/
  179.  
  180.  
  181. ULONG
  182. LTP_LevelGadgetHandleInput(struct Gadget *Gadget,struct gpInput *InputMsg)
  183. {
  184.     if(InputMsg->gpi_IEvent->ie_Class == IECLASS_RAWMOUSE)
  185.     {
  186.         LevelExtra        *Special = Gadget->SpecialInfo;
  187.         LONG             Left,Width;
  188.         ULONG             KnobWidth,
  189.                          Position,
  190.                          Current,
  191.                          Max,
  192.                          Result;
  193.         struct RastPort *RPort;
  194.         BOOL             DrawIt;
  195.  
  196.         GetAttr(LVIA_Position,    Special->LevelImage,&Position);
  197.         GetAttr(LVIA_KnobWidth, Special->LevelImage,&KnobWidth);
  198.         GetAttr(LVIA_Max,        Special->LevelImage,&Max);
  199.  
  200.         Left = InputMsg->gpi_Mouse.X + Special->MidOffset - KnobWidth;
  201.  
  202.         Width = Special->LevelImage->Width - 2 * KnobWidth;
  203.  
  204.         if(InputMsg->gpi_IEvent->ie_Code == SELECTUP)
  205.         {
  206.             *InputMsg->gpi_Termination = FALSE;
  207.  
  208.             Result = GMR_NOREUSE | GMR_VERIFY;
  209.         }
  210.         else
  211.             Result = 0;
  212.  
  213.         if(Left < 0)
  214.             Left = 0;
  215.         else
  216.         {
  217.             if(Left > Width)
  218.                 Left = Width;
  219.         }
  220.  
  221.         if(Max)
  222.         {
  223.             Current = (Left * Max + (Width / (Max * 2))) / Width;
  224.  
  225.             if(Current > Max)
  226.                 Current = Max;
  227.             else
  228.             {
  229.                 if(Current < 0)
  230.                     Current = 0;
  231.             }
  232.         }
  233.         else
  234.             Current = 0;
  235.  
  236.         SetAttrs(Special->LevelImage,LVIA_Current,Current,TAG_DONE);
  237.  
  238.         if(Result)
  239.             DrawIt = TRUE;
  240.         else
  241.         {
  242.             if(Position != Left && Max)
  243.             {
  244.                 DrawIt = TRUE;
  245.  
  246.                 SetAttrs(Special->LevelImage,
  247.                     LVIA_Position,    Left,
  248.                 TAG_DONE);
  249.             }
  250.             else
  251.                 DrawIt = FALSE;
  252.         }
  253.  
  254.         if(DrawIt)
  255.         {
  256.             if(RPort = ObtainGIRPort(InputMsg->gpi_GInfo))
  257.             {
  258.                 DrawImageState(RPort,Special->LevelImage,Gadget->LeftEdge,Gadget->TopEdge,Result ? IDS_NORMAL : IDS_SELECTED,Special->Handle->DrawInfo);
  259.  
  260.                 ReleaseGIRPort(RPort);
  261.             }
  262.  
  263.             LTP_LevelGadgetDrawLabel(Gadget,FALSE);
  264.         }
  265.  
  266.         return(Result);
  267.     }
  268.     else
  269.         return(0);
  270. }
  271.  
  272.  
  273. /*****************************************************************************/
  274.  
  275.     // Note: this is not a class dispatcher, it's a custom gadget dispatcher
  276.  
  277. ULONG __saveds __asm
  278. LTP_LevelGadgetDispatcher(REG(a0) struct Hook *Hook,REG(a2) struct Gadget *Gadget,REG(a1) Msg msg)
  279. {
  280.     switch(msg->MethodID)
  281.     {
  282.         case GM_HITTEST:
  283.             return(GMR_GADGETHIT);
  284.  
  285.         case GM_RENDER:
  286.             return(LTP_LevelGadgetRender(Gadget,(struct gpRender *)msg));
  287.  
  288.         case GM_GOACTIVE:
  289.             return(LTP_LevelGadgetGoActive(Gadget,(struct gpInput *)msg));
  290.  
  291.         case GM_HANDLEINPUT:
  292.             return(LTP_LevelGadgetHandleInput(Gadget,(struct gpInput *)msg));
  293.  
  294.         default:
  295.             return(0);
  296.     }
  297. }
  298. #endif /* DO_LEVEL_KIND */
  299.